local assets = {
	Asset("ANIM", "anim/musha_phoenixaxe.zip"),
	Asset("ANIM", "anim/musha_phoenixaxe_swap.zip"),
	Asset("ANIM", "anim/musha_phoenixpick_swap.zip"),
	Asset("ANIM", "anim/musha_phoenixaxe_broken_swap.zip")
}
---------------------------

local function createlight(inst, target, pos)
	if inst.components.musha_equipment:IsBroken() then
		return
	end
	local caster = inst.components.inventoryitem.owner
	if caster.components.sanity.current >= 35 then
		local light = SpawnPrefab("lightning")
		local light2 = SpawnPrefab("musha_spore_fire")

		light.Transform:SetPosition(pos.x, pos.y, pos.z)
		light2.Transform:SetPosition(pos.x, pos.y, pos.z)
		light2.components.follower:SetLeader(caster)

		caster.components.sanity:DoDelta(-20)
	elseif caster.components.sanity.current < 30 then
		local fail1 = SpawnPrefab("statue_transition")
		local fail2 = SpawnPrefab("statue_transition_2")

		fail1.Transform:SetPosition(pos.x, pos.y, pos.z)
		fail2.Transform:SetPosition(pos.x, pos.y, pos.z)
	end
end

local function Updata(inst, display)
	if inst.components.musha_equipment:IsBroken() then
		inst.components.weapon:SetDamage(1)
		return
	end
	inst.components.weapon:SetDamage((inst.is_Pick and 73 or 28) + inst.components.musha_equipment.currentlevel * 2)
	if display then
		inst.components.talker:Say(
			string.format(
				(inst.is_Pick and STRINGS.MUSHA_WEAPON_SWORD_PAXE or STRINGS.MUSHA_WEAPON_SWORD_AXE) ..
					" (LV%d)\n" .. STRINGS.MUSHA_WEAPON_DAMAGE .. " (%d)",
				inst.components.musha_equipment.currentlevel,
				(inst.is_Pick and 73 or 28) + inst.components.musha_equipment.currentlevel * 2
			)
		)
	end
end

local function OnAttack(inst, attacker, target)
	if inst.components.musha_equipment:IsBroken() then
		return
	end

	if target then
		local splash = SpawnPrefab("splash")
		local pos = Vector3(target.Transform:GetWorldPosition())
		splash.Transform:SetPosition(pos:Get())

		if
			target.components.health and not target.components.health:IsDead() and not target:HasTag("structure") and
				not target:HasTag("stalkerminion") and
				not target:HasTag("smashable") and
				not target:HasTag("alignwall") and
				not target:HasTag("shadowminion")
		 then
			if
				math.random() <
					((inst.components.musha_equipment.currentlevel < 10 and 0.12) or
						(inst.components.musha_equipment.currentlevel < 20 and 0.20) or
						(inst.components.musha_equipment.currentlevel < 24 and 0.28) or
						0.36)
			 then
				local fx = SpawnPrefab("firesplash_fx")
				fx.Transform:SetScale(0.4, 0.4, 0.4)
				fx.Transform:SetPosition(target:GetPosition():Get())
				if target.components.burnable then
					target.components.burnable:Ignite()
				end
				local fx2 = SpawnPrefab("statue_transition")
				local pos = Vector3(target.Transform:GetWorldPosition())
				fx2.Transform:SetPosition(pos:Get())
				target.components.health:DoDelta(
					inst.components.musha_equipment.currentlevel < 24 and
						-5 - math.ceil(inst.components.musha_equipment.currentlevel * 0.1) * 5 or
						25
				)
				attacker.components.sanity:DoDelta(1)
			end
		end
	end

	if math.random() < 0.05 and inst.components.musha_equipment.currentexp < 4000 then
		inst.components.musha_equipment.currentexp = inst.components.musha_equipment.currentexp + 1
		Updata(inst, false)
	end
end

local function OnEquip(inst, owner)
	--Updata(inst)

	if inst.is_Pick then
		inst.components.weapon:SetRange(1)
		owner.AnimState:OverrideSymbol("swap_object", "musha_phoenixpick_swap", "phoenixaxe")
		if not inst.components.spellcaster then
			inst:AddComponent("spellcaster")
			inst.components.spellcaster:SetSpellFn(createlight)
			inst.components.spellcaster.canuseonpoint = true
		end
	else
		inst.components.weapon:SetRange(0)
		owner.AnimState:OverrideSymbol("swap_object", "musha_phoenixaxe_swap", "phoenixaxe")
	end
	owner.AnimState:Show("ARM_carry")
	owner.AnimState:Hide("ARM_normal")
end

local function OnUnequip(inst, owner)
	owner.AnimState:Hide("ARM_carry")
	owner.AnimState:Show("ARM_normal")

	inst:RemoveComponent("spellcaster")
end

local function Transform(inst)
	local owner = inst.components.inventoryitem.owner

	local change_fx = SpawnPrefab("small_puff")
	local puff = change_fx.entity:AddFollower()
	puff:FollowSymbol(owner.GUID, "swap_object", 1, -150, 0.5)

	inst.SoundEmitter:PlaySound("dontstarve/common/gem_shatter")
	if inst.is_Pick then
		inst:RemoveComponent("spellcaster")
		inst.components.tool:OnRemoveFromEntity()
		inst.components.tool:SetAction(ACTIONS.CHOP)
		inst.components.weapon:SetRange(0)
		owner.AnimState:OverrideSymbol("swap_object", "musha_phoenixaxe_swap", "phoenixaxe")
	else
		inst:AddComponent("spellcaster")
		inst.components.spellcaster:SetSpellFn(createlight)
		inst.components.spellcaster.canuseonpoint = true
		inst.components.tool:OnRemoveFromEntity()
		inst.components.tool:SetAction(ACTIONS.MINE)

		inst.components.weapon:SetRange(.9)
		owner.AnimState:OverrideSymbol("swap_object", "musha_phoenixpick_swap", "phoenixaxe")
	end
	Updata(inst)
	inst.is_Pick = not inst.is_Pick
	inst.components.useableitem:StopUsingItem()
	--#TODO:添加提示
end

local function fn()
	local inst = CreateEntity()

	inst.entity:AddTransform()
	inst.entity:AddAnimState()
	inst.entity:AddSoundEmitter()
	inst.entity:AddNetwork()

	inst.entity:AddLight()
	inst.Light:SetRadius(.4)
	inst.Light:SetFalloff(.5)
	inst.Light:SetIntensity(.5)
	inst.Light:SetColour(80 / 255, 120 / 255, 250 / 255)
	inst.Light:Enable(true)

	inst.AnimState:SetBank("musha_phoenixaxe")
	inst.AnimState:SetBuild("musha_phoenixaxe")
	inst.AnimState:PlayAnimation("idle")
	inst.AnimState:SetMultColour(1, 1, 1, 0.7)

	inst:AddTag("sharp")
	inst:AddTag("metal")
	inst:AddTag("musha_items")

	inst:AddComponent("talker")
	inst.components.talker.fontsize = 30
	inst.components.talker.font = TALKINGFONT
	inst.components.talker.colour = Vector3(0.9, 1, 0.75, 1)
	inst.components.talker.offset = Vector3(0, 150, 0)
	inst.components.talker.symbol = "swap_object"

	MakeInventoryPhysics(inst)
	MakeInventoryFloatable(inst, nil, nil)

	inst.entity:SetPristine()

	if TheNet:GetIsClient() then
		return inst
	end

	inst:AddComponent("tool")

	inst:AddComponent("useableitem")
	inst.components.useableitem:SetOnUseFn(Transform)

	inst:AddComponent("equippable")
	inst.components.equippable:SetOnEquip(OnEquip)
	inst.components.equippable:SetOnUnequip(OnUnequip)

	inst:AddComponent("musha_equipment")
	inst.components.musha_equipment:SetUpgradable(false)
	inst.components.musha_equipment:InitMushaEquip("weapon", 750, 0)
	inst.components.musha_equipment:SetOnUpdataFn(Updata)
	inst.components.musha_equipment:SetOnBrokenFn(
		function()
			inst.components.inventoryitem.owner.components.talker:Say(STRINGS.MUSHA_WEAPON_BROKEN_TALK)
			inst.components.talker:Say("-" .. STRINGS.MUSHA_WEAPON .. "\n" .. STRINGS.MUSHA_ITEM_DUR .. " (0)")
			Updata(inst)
		end
	)

	--#TODO:凤凰稿的装备栏
	inst.components.inventoryitem.atlasname = "images/inventoryimages/musha_phoenixaxe.xml"

	inst.BuffAttack = OnAttack

	inst.is_Pick = false
	inst.type = "phoenix"

	inst.OnSave = function(inst, data)
		data.exp = inst.components.musha_equipment.currentexp
		data.is_Pick = inst.is_Pick
	end

	inst.OnLoad = function(inst, data)
		if data ~= nil then
			if data.is_Pick ~= nil then
				inst.is_Pick = data.is_Pick
			end
		end
	end

	return inst
end

return Prefab("musha_phoenixaxe", fn, assets)
